package org.noote.libs.widget;

import javax.microedition.khronos.opengles.GL10;

public class Widget_Icon extends Widget_Window {

	protected int _iIconPosition_X = 64, _iIconPosition_Y = 64;
	protected int _iIconSize_X = 64, _iIconSize_Y = 64;
	protected WidgetSquare2D _squareIcon = null;
	
	protected float _colorIcon[] = {1, 1, 1, 1};
	protected float _colorSelectedIcon[] = {1, 1, 1, 1};
	protected float _colorDisabledIcon[] = {0.65f, 0.65f, 0.7f, 1};
	
	protected boolean _bOnOffMode=false;
	protected boolean _bIsOn=false;
	
	protected boolean _bTouchMode=true;
	
	public Widget_Icon(Widget_Window parent) {
		super(parent);
	}
	
	public Widget_Icon(Widget_Window parent, WidgetTheme theme) {
		super(parent, theme);
	}
	
	@Override
	public void create()
	{
		super.create();
		
		if(_theme != null)
		{
			_colorIcon = _theme.colorIconDefault;
			_colorSelectedIcon = _theme.colorIconSelected;
			_colorDisabledIcon = _theme.colorIconDisabled;
		}
		
		setWorkSpace(0, 0, _theme.padSizeX, _theme.padSizeY);
		setIconPosition(_theme.padSizeX/2, _theme.padSizeY/2);
	}
	
	public void setIcon(int texID) {
		setIcon(texID, _theme.iconSizeX, _theme.iconSizeY);
	}
	public void setIcon(int texID, int sx, int sy) {
		if(_squareIcon == null)
			_squareIcon = new WidgetSquare2D(texID);
		else
			_squareIcon.setTextureID(texID);
		_squareIcon.setSize(sx, sy, 0);
		_iIconSize_X = sx;
		_iIconSize_Y = sy;
		
		refresh();
	}
	public void setIconPosition(int px, int py) {
		_iIconPosition_X = px;
		_iIconPosition_Y = py;
	}
	public void setIconColor(float color[]) {
		_colorIcon = color;
		refresh();
	}
	public void setIconColor(float r, float g, float b) {
		setIconColor(r, g, b, 1);
	}
	public void setIconColor(float r, float g, float b, float a) {
		float color[] = {r, g, b, a};
		setIconColor(color);
	}
	public void setSystemIcon(int iIdx)
	{
		setIcon(_theme._icons.getTextureID(iIdx), _theme.iconSizeX, _theme.iconSizeY);
		setIconColor(_theme.colorGray);
	}
	
	@Override
	protected void _postRefresh()
	{
		super._postRefresh();
		
		if(_squareIcon != null)
		{
			if(_bEnabled && _bSelected)
				_squareIcon.setColor(_colorSelectedIcon);
			else if(_bEnabled)
				_squareIcon.setColor(_colorIcon);
			else
				_squareIcon.setColor(_colorDisabledIcon);
		}
	}
	
	
	@Override
	protected void _drawElements(GL10 gl, float fDepth) {
		super._drawElements(gl, fDepth);
		
		if (_squareIcon != null) {
			gl.glPushMatrix();
			gl.glTranslatef(_iIconPosition_X-_iIconSize_X/2, _iIconPosition_Y-_iIconSize_Y/2, fDepth+0.5f);
			_squareIcon.draw(gl);
			gl.glPopMatrix();
		}
	}
}
